package com.jade.explorer.dirscan;

import java.io.File;
import java.util.ArrayList;

public class WorkspaceScanner {
	
	//------------------------------Private fields
	/** Contains path to workspace directory */
	private String _workspace = "";
	/** Contains all project from directory*/
	private ArrayList<Project> _projectList = new ArrayList<Project>();
	
	
	//------------------------------Private methods
	/** Method scans selected directory for .project file
	 *  and validate it
	 * @param file
	 * @return <code>true</code> if directory consist .project file
	 */
	private boolean scanProjectFile(File file){
		
		for(int i = 0; i < file.list().length; i++){
			if ((file.list()[i].toLowerCase().indexOf(".project")) >= 0)
				return true;	//TODO validate project file
		}
		return false;
	}
	
	
	//------------------------------Constructors
	public WorkspaceScanner(String workspace){
		_workspace = workspace;
		File file = new File(workspace);
		for(int i = 0; i < file.list().length; i++){
			File f = new File(file.listFiles()[i].getPath());
			if (scanProjectFile(f)){
				DirScanner ds = new DirScanner(file.listFiles()[i].getPath());
				_projectList.add(ds.getStructure());
			}
			
		}
	}
	
	
	//------------------------------Public methods
	public void refresh(){
		_projectList.clear();
		File file = new File(_workspace);
		for(int i = 0; i < file.list().length; i++){
			File f = new File(file.listFiles()[i].getPath());
			if (scanProjectFile(f)){
				DirScanner ds = new DirScanner(file.listFiles()[i].getPath());
				_projectList.add(ds.getStructure());
			}
		}
	}
	
	public String getWorkspaceDir(){
		return _workspace;
	}
	
	
	public void setWorkspaceDir(String workspace){
		_workspace = workspace;
	}
	
	public int count(){
		return _projectList.size();
	}
	
	public ArrayList<Project> ProjectList(){
		return _projectList;
	}
}
